#!/bin/sh
PORT=$1

SERVER_PORT=0

if test -z ${PORT}
then
    SERVER_PORT="10100"
else
    SERVER_PORT=${PORT}
fi

SERVER_NAME="eureka-server"

CONTAINER_NAME=${SERVER_NAME}-${SERVER_PORT}
echo ${CONTAINER_NAME}

echo "执行脚本"
git pull
mvn clean
echo "删除旧版jar包"
rm -rf target/

echo "重新打包"
mvn -U clean compile package -Dmaven.test.skip=true

echo "停止旧版服务"
docker stop ${CONTAINER_NAME}
docker rm ${CONTAINER_NAME}

echo "删除旧版镜像"
docker rmi ${SERVER_NAME}

echo "部署新版本镜像"
docker build -t ${SERVER_NAME} -f script/Dockerfile  .

echo "启动新服务"
docker run -ti -d --net host --name ${CONTAINER_NAME} --publish ${SERVER_PORT}:${SERVER_PORT} --env SERVER_PORT=${SERVER_PORT} ${SERVER_NAME}

echo "打印服务日志"
docker logs -f ${CONTAINER_NAME}


